package photogcalc;

import java.util.Vector;
import photogcalc.calculators.DofController;
import photogcalc.i18n.I18n;
import photogcalc.i18n.I18nFactory;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Screen;
import javax.microedition.midlet.MIDlet;
import net.sf.microlog.core.Logger;
import net.sf.microlog.core.LoggerFactory;
import photogcalc.calculators.AovController;
import photogcalc.calculators.CalculatorController;
import photogcalc.calculators.GnController;

/**
 *
 * @author aokkonen
 */
public class MainController extends MIDlet implements MainCommandListener, ChildFormListener {

    private PropertiesController propertiesController = null;
    private AboutController aboutController = null;
    private Vector calculators = null;

    private MainModel model = null;
    private MainView view = null;

    /** midlet's display */
    private Display display;

    /** screen to resume after pause */
    private Screen resumeFrom;

    /** logger */
    private Logger logger = LoggerFactory.getLogger(getClass().getName());
    
    /** Inner confirm screen class. */
    class ConfirmScreen extends Alert implements CommandListener {

        private Command ok;
        private Command cancel;

        public ConfirmScreen(String title, String msg) {
            super(title);
            setString(msg);
            setTimeout(FOREVER);

            I18n i18n = I18nFactory.createI18n(System.getProperty("microedition.locale"));
            ok = new Command(i18n.get("_Poista"), Command.OK, 1);
            cancel = new Command(i18n.get("_Peruuta"), Command.BACK, 2);

            addCommand(ok);
            addCommand(cancel);
            setCommandListener(this);
        }

        public void commandAction(Command c, Displayable d) {
            if (c == cancel) {
                display(view.getScreen());
            } else if (c == ok) {
                //Settings.deleteRecordStore("CameraCalculator");
                exitCommand();
            }
        }
    }
    
    protected void startApp() {
        logger.debug("startApp");

	if (display == null) {
            model = new MainModel();
            view = new MainView(this);

            resumeFrom = view.getScreen();
            display = Display.getDisplay(this);

            propertiesController = new PropertiesController(this, model.getSettings());
            aboutController = new AboutController(this);

            calculators = new Vector(3);
            calculators.addElement(
                    new DofController(this, model.getSettings()));
            calculators.addElement(
                    new AovController(this, model.getSettings()));
            calculators.addElement(
                    new GnController(this, model.getSettings()));

            view.initData(calculators);
        }

        display(resumeFrom);
    }

    protected void pauseApp() {
        // TODO: manage pause state
        logger.debug("pauseApp");
    }

    protected void destroyApp(boolean unconditional) {
        logger.debug("destroyApp");
        LoggerFactory.shutdown();
        notifyDestroyed();
    }

    /** Sets screen visible. This method is called from the child Form
        outside this class and by the object created from this class.
        @param scr Screen to display.
    */
    public void display(Screen scr) {
        resumeFrom = scr;
        display.setCurrent(scr);
    }

    /** This method is called from the child form outside this class. */
    public void signalExit() {
        display(view.getScreen());
    }

    public void deleteRecordStoreCommand() {
        logger.debug("deleteRecordStroreCommand");
        ConfirmScreen notify = new ConfirmScreen("Huomio!", "Poistetaanko asetukset");
        display(notify);
        exitCommand();
    }

    public void calculatorCommand(int calculator) {
        CalculatorController bc = ((CalculatorController)calculators.elementAt(calculator));
        bc.refreshScreen();
        display(bc.getScreen());
    }

    public void propertiesCommand() {
        display(propertiesController.getScreen());
    }

    public void aboutCommand() {
//        I18n i18n = I18nFactory.createI18n(System.getProperty("microedition.locale"));
//
//        String title =
//                i18n.get("_Ohjelmatiedot") +
//                " - " +
//                Constants.TITLE;
//
//        String version =
//                Constants.TITLE +
//                " " +
//                Constants.VERSION +
//                " (" +
//                Constants.RELEASE_DATE +
//                ") \n" +
//                Constants.COPYRIGHT +
//                " \n\n" +
//                Constants.LICENSE;
//
//        Alert about = new Alert(
//                title,
//                version,
//                null,
//                AlertType.INFO);
//
//        about.setTimeout(Alert.FOREVER);
//        display.setCurrent(about, resumeFrom);

        display(aboutController.getScreen());

    }

    public void exitCommand() {
        logger.debug("exitCommand");
        model.writeSettings();
        destroyApp(false);
    }
}
